﻿using ezCloud.Api.Services;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;

namespace ezCloud.Api.Common
{
    public class EmailSender
    {
        public static async Task SendPaymentStatus( string email, string subject, string Fullname, string Amount, string Status,string StatusEN,int ezPaymentLogId)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailPaymentStatus.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{Amount}}", Amount)
                .Replace("{{Status}}", Status)
                .Replace("{{StatusEN}}", StatusEN)
                .Replace("{{ezPaymentLogId}}", ezPaymentLogId.ToString())
                .Replace("{{pmsurl}}", PmsUrl);
                ;
            SendEmail(email, subject, emailTemplate);

            SendEmail("partner@ezcloud.vn", subject, emailTemplate);

            SendEmail("ketoan@ezcloud.vn", subject, emailTemplate);

            SendEmail("founder@ezcloud.vn", subject, emailTemplate);
        }
        public static async Task SendEmail(string email, string subject, string body)
        {
            EmailService emailService = new EmailService();
            IdentityMessage message = new IdentityMessage();
            message.Destination = email;
            message.Subject = subject;
            message.Body = body;
            emailService.SendGmailAsync(message);
        }

        public static async Task SendEmailHotelExpiredDateCountDown(string email, string subject, string Fullname, string RemainingDays, int hotelId, string HotelName)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("/EmailTemplates/emailHotelExpiredDateCountDown.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{RemainingDays}}", RemainingDays)
                .Replace("{{HotelId}}", hotelId.ToString())
                .Replace("{{HotelName}}", HotelName)
                .Replace("{{pmsurl}}", PmsUrl)
            ;
            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailHotelAutoAssignRoomUnsuccessful(string email, string subject, string Fullname, string CMChannelRef,string Channel,string GuestName, DateTime ArrivalDate,DateTime DepartureDate,String RoomType)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("/EmailTemplates/emailCMErrorAutoAssignRoom.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{CMChannelRef}}", CMChannelRef)
                .Replace("{{Channel}}", Channel)
                .Replace("{{GuestName}}", GuestName)
                .Replace("{{ArrivalDate}}", ArrivalDate.ToString("dd/MM/yyyy"))
                .Replace("{{DepartureDate}}", DepartureDate.ToString("dd/MM/yyyy"))
                .Replace("{{RoomType}}", RoomType)
                .Replace("{{pmsurl}}", PmsUrl)
            ;
            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailCreatedHotel(string email, string subject, string Fullname, int hotelId, string HotelName, DateTime fromDate, DateTime toDate)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailHotelCreated.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                                        .Replace("{{HotelId}}", hotelId.ToString())
                                        .Replace("{{HotelName}}", HotelName)
                                        .Replace("{{FromDate}}", fromDate.ToString("dd/MM/yyyy"))
                                        .Replace("{{ToDate}}", toDate.ToString("dd/MM/yyyy"))
                                        .Replace("{{pmsurl}}", PmsUrl)
                                        ;
            SendEmail(email, subject, emailTemplate);
        }
        public static async Task SendEmailIfCMProcessInventoryFalse(string email, string subject, string Fullname, int hotelId, string HotelName, string ListInventoryErrorVN, string ListInventoryErrorEN)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("/EmailTemplates/emailCMError.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                                        .Replace("{{HotelId}}", hotelId.ToString())
                                        .Replace("{{HotelName}}", HotelName)
                                        .Replace("{{ListInventoryErrorVN}}", ListInventoryErrorVN)
                                        .Replace("{{ListInventoryErrorEN}}", ListInventoryErrorEN)
                                        .Replace("{{pmsurl}}", PmsUrl)
                                        ;
            SendEmail(email, subject, emailTemplate);

            //SendEmail("founder@ezcloud.vn", subject, emailTemplate);
        }
        public static async Task SendEmailConfirmation(string email, string Password, string subject, string Fullname, string callbackUrl)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailConfirmation.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{Username}}", email).Replace("{{Password}}", Password)
                .Replace("{{pmsurl}}", PmsUrl)
                .Replace("{{ConfirmationLink}}", callbackUrl);
            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailResetPassword(string email, string subject, string Fullname, string callbackUrl)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailResetPassword.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{pmsurl}}", PmsUrl)
                .Replace("{{ConfirmationLink}}", callbackUrl);
            
            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailChangeEmailHotelOwner(string email, string subject, string Fullname, string NewEmail, string OldEmail)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailChangeEmailHotelOwner.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{newEmail}}", NewEmail)
                .Replace("{{oldEmail}}", OldEmail)
                .Replace("{{pmsurl}}", PmsUrl);

            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailChangeEmailHotelOwnerToOldOwner(string email, string subject, string Fullname, string NewEmail, string OldEmail)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailChangeEmailHotelOwnerToOldOwner.html"));
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{newEmail}}", NewEmail)
                .Replace("{{oldEmail}}", OldEmail)
                .Replace("{{pmsurl}}", PmsUrl);

            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailReferralCode(string email, string Fullname, string ReferralCode, string HotelName, int HotelId)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string subject = "ezCloudHotel.com - Referral Code/Mã giới thiệu:" + HotelName + "(HotelId:" + HotelId.ToString() + ")";
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailReferralCode.html"));
            string referralSubject= "Your hotel friend " + HotelName + " send to you a referral code: " + ReferralCode+"/Khách sạn " + HotelName + " gửi mã giới thiệu:"+ReferralCode;
            string referralBody= "Bạn sẽ nhận được 5% số tiền khuyến mại cho lần thanh toán trực tuyến đầu tiên. Hãy nhập mã giới thiệu " + ReferralCode + " để gia tăng thời hạn sử dụng phần mềm. / Give a hotel friend 5% off their first payment with your referral code and get rewarded with 5% off your first payment. Make sure your hotel friends are ok to receive communications from you.";
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{ReferralCode}}", ReferralCode)
                .Replace("{{referralSubject}}", referralSubject)
                .Replace("{{referralBody}}", referralBody)
                .Replace("{{pmsurl}}", PmsUrl);

            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailReferralCommision_A(string email,  string Fullname, string ReferralCode_A, string HotelName_A,int HotelId_A, string HotelName_B, string CommisionAmount_A, string TotalAmount)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string subject = HotelName_A + "(HotelId:"+HotelId_A.ToString()+") - You just received " + CommisionAmount_A + " VNĐ promotional funds from the referral program / Bạn vừa nhận được " + CommisionAmount_A + " VNĐ tiền khuyến mại từ chương trình giới thiệu";
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/emailReferralCommisionToA.html"));
            string referralSubject = "Khách sạn " + HotelName_A + " gửi mã giới thiệu:" + ReferralCode_A + "/ Your hotel friend " + HotelName_A + " send to you a referral code: " + ReferralCode_A;
            string referralBody = "Bạn sẽ nhận được 5% số tiền khuyến mại cho lần thanh toán trực tuyến đầu tiên. Hãy nhập mã giới thiệu " + ReferralCode_A + " để gia tăng thời hạn sử dụng phần mềm. / Give a hotel friend 5% off their first payment with your referral code and get rewarded with 5% off your first payment. Make sure your hotel friends are ok to receive communications from you.";
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{CommisionAmount_A}}", CommisionAmount_A)
                 .Replace("{{HotelName_B}}", HotelName_B)
                .Replace("{{TotalAmount}}", TotalAmount)
                .Replace("{{ReferralCode}}", ReferralCode_A)
                .Replace("{{referralSubject}}", referralSubject)
                .Replace("{{referralBody}}", referralBody)
                .Replace("{{pmsurl}}", PmsUrl);

            SendEmail(email, subject, emailTemplate);
        }

        public static async Task SendEmailReferralCommision_B(string email, string Fullname, string ReferralCode_A,  string HotelName_A, string HotelName_B, int HotelId_B, string CommisionAmount_B, string TotalAmount)
        {
            string PmsUrl = WebConfigurationManager.AppSettings["RootUrl"];
            string subject = HotelName_B + "(HotelId:" + HotelId_B.ToString() + ") - You just received " + CommisionAmount_B + " VNĐ promotional funds from the referral program / Bạn vừa nhận được " + CommisionAmount_B + " VNĐ tiền khuyến mại từ chương trình giới thiệu";
            string emailTemplate = File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates//emailReferralCommisionToB.html"));
           
            emailTemplate = emailTemplate.Replace("{{Fullname}}", Fullname)
                .Replace("{{CommisionAmount_B}}", CommisionAmount_B)
                .Replace("{{ReferralCode_A}}", ReferralCode_A)
                .Replace("{{HotelName_A}}", HotelName_A)
                .Replace("{{TotalAmount}}", TotalAmount)
                .Replace("{{pmsurl}}", PmsUrl);

            SendEmail(email, subject, emailTemplate);
        }

    }
}